import { defineStore } from 'pinia'
// 自己的方式
export const useCounterStore = defineStore('useCounterStore', {
  state() {
    return {
      num: 0
    }
  },
  actions: {
    subtract() {
      if(this.num <= 1) return
      this.num--;
    },
    increment() {
      this.num++;
    },
    // 异步加
    async asyncIncrement() {
      await new Promise(resolve => setTimeout(resolve,1000))
      this.increment()
    },
    // 异步减
    async asyncDecrement() {
      await new Promise(resolve => setTimeout(resolve,1000))
      this.subtract()
    }
  },
  getters: {
    doubleCount: (state) => state.num * 2
  }
})